#include <iostream>
#include <list>
#include <vector>

using std::cout;
using std::endl;
using std::list;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    list<int> number = {1, 3, 5, 7, 9, 4, 6, 8, 2};
    display(number);

    cout << endl << "在list的尾部进行插入与删除" << endl;
    number.push_back(100);
    number.push_back(200);
    display(number);
    number.pop_back();
    display(number);

    cout << endl << "在list的头部进行插入与删除" << endl;
    number.push_front(111);
    number.push_front(222);
    display(number);
    number.pop_front();
    display(number);

    cout << endl << "在list的任意位置进行插入" << endl;
    list<int>::iterator it = number.begin();
    /* it +=2;//error,不支持这个语法 */
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 300);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl;
    number.insert(it, 5, 100);
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl;
    vector<int> vec = {11, 33, 55, 88, 22};
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl;
    number.insert(it, {123, 567, 678, 345});
    display(number);
    cout << "*it = " << *it << endl;

    cout << endl << "list清空元素" << endl;
    number.clear();//清空元素
    cout << "size() = " << number.size() << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

